package frank.zou.study.testng.extending.methodSelector;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Calendar;
import java.util.List;

import org.testng.IMethodSelector;
import org.testng.IMethodSelectorContext;
import org.testng.ITestNGMethod;
import org.testng.annotations.Test;

public class MyMethodSelector implements IMethodSelector {

	public boolean includeMethod(IMethodSelectorContext context,
			ITestNGMethod method, boolean isTest) {
		
		int day = Calendar.getInstance().get(Calendar.DAY_OF_WEEK);
		String group = day == Calendar.SATURDAY || day == Calendar.SUNDAY ? "weekend"
				: "week";
		System.out.println(method.getMethodName());
		Method tm = method.getMethod();
		Annotation a = tm.getAnnotation(Test.class);
		if (a != null) {
			Test test = (Test) a;
			String[] groups = test.groups();
			for (String g : groups) {
				if (g.equals(group)) {
					System.out.println(": method selector returning true");
					return true;
				}
			}
		}
		System.out.println(": method selector returning false");
		return false;

	}

	public void setTestMethods(List<ITestNGMethod> arg0) {
		// TODO Auto-generated method stub

	}

}
